package com.biyabi.service.impl;

import com.biyabi.dao.UserMapper;
import com.biyabi.entity.User;
import com.biyabi.entity.UserExample;
import com.biyabi.pojo.UsersData;
import com.biyabi.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public UsersData findAll(Integer currentPage, Integer pageSize, String columnName, String columnValue){
        currentPage = currentPage == null ? 1:currentPage;
        pageSize= pageSize== null ? 5:pageSize;
        UserExample userExample = new UserExample();
        if(columnName!=null && columnValue!=null){
            switch (columnName){
                case "phone":
                    userExample.createCriteria().andPhoneLike("%"+columnValue+"%");
                    break;
                case "nickname":
                    userExample.createCriteria().andNicknameLike("%"+columnValue+"%");
                    break;
                case"level":
                    userExample.createCriteria().andLevelEqualTo(Integer.parseInt(columnValue));
                    break;
                case"score":
                    userExample.createCriteria().andScoreEqualTo(Integer.parseInt(columnValue));
                    break;
                case"status":
                    userExample.createCriteria().andStatusEqualTo("0".equals(columnValue)?0:1);
                    break;
            }
        }
        UsersData usersData = new UsersData();
        usersData .setTotal(userMapper.countByExample(userExample));
        PageHelper.startPage(currentPage,pageSize);
        List<User> users = userMapper.selectByExample(userExample);
        usersData .setRows(users);
        return usersData ;
    }

    public void updateUserStatus(String id,Integer stutas) {
        User user = userMapper.selectByPrimaryKey(id);
        user.setStatus(stutas);
        userMapper.updateByPrimaryKey(user);
    }


    public User login(User user){
        UserExample example = new UserExample();
        example.createCriteria().andPhoneEqualTo(user.getPhone());
        List<User> users = userMapper.selectByExample(example);
        if(users.size()>0){
            User user1 = users.get(0);
            if(user.getPassword().equals(DigestUtils.md5DigestAsHex((user1.getSalt()+user.getPassword()).getBytes())))
                return user1;
        }
        return null;
    }
    public void regist(User user){
        user.setId(UUID.randomUUID().toString());
        userMapper.insert(user);
    }

    public User findOne(String userId){
        return userMapper.selectByPrimaryKey(userId);
    }
    public void uploadPhoto(MultipartFile photo, String userId, HttpServletRequest request){
        try {
            String realPath = request.getSession().getServletContext().getRealPath("/upload/user");
            photo.transferTo(new File(realPath,photo.getOriginalFilename()));
            User user = userMapper.selectByPrimaryKey(userId);
            user.setPhoto("/upload/user"+photo.getOriginalFilename());
            userMapper.updateByPrimaryKey(user);
        } catch (IOException e) {
            throw new RuntimeException();
        }
    }

}
